home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / inflow.c,v < prev    next >
Encoding:
Text File  |  1990-12-10  |  13.5 KB  |  605 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.12.09.22.36.05;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.11.12.16.13.35;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Changes for Sprite. (Mike checking in for Bob.)
  27. @
  28. text
  29. @/* Low level interface to ptrace, for GDB when running under Unix.
  30.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  31.  
  32. This file is part of GDB.
  33.  
  34. GDB is free software; you can redistribute it and/or modify
  35. it under the terms of the GNU General Public License as published by
  36. the Free Software Foundation; either version 1, or (at your option)
  37. any later version.
  38.  
  39. GDB is distributed in the hope that it will be useful,
  40. but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42. GNU General Public License for more details.
  43.  
  44. You should have received a copy of the GNU General Public License
  45. along with GDB; see the file COPYING.  If not, write to
  46. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  47.  
  48. #include <stdio.h>
  49. #include "defs.h"
  50. #include "param.h"
  51. #include "frame.h"
  52. #include "inferior.h"
  53.  
  54. #ifdef USG
  55. #include <sys/types.h>
  56. #endif
  57.  
  58. /* Some USG-esque systems (some of which are BSD-esque enough so that USG
  59.    is not defined) want this header, and it won't do any harm.  */
  60. #include <fcntl.h>
  61.  
  62. #include <sys/param.h>
  63. #include <sys/dir.h>
  64. #include <signal.h>
  65.  
  66. #ifdef HAVE_TERMIO
  67. #include <termio.h>
  68. #undef TIOCGETP
  69. #define TIOCGETP TCGETA
  70. #undef TIOCSETN
  71. #define TIOCSETN TCSETA
  72. #undef TIOCSETP
  73. #define TIOCSETP TCSETAF
  74. #define TERMINAL struct termio
  75. #else
  76. #include <sys/ioctl.h>
  77. /* #include <fcntl.h> */
  78. #include <sgtty.h>
  79. #define TERMINAL struct sgttyb
  80. #endif
  81.  
  82. #ifdef SET_STACK_LIMIT_HUGE
  83. #include <sys/time.h>
  84. #include <sys/resource.h>
  85. extern int original_stack_limit;
  86. #endif /* SET_STACK_LIMIT_HUGE */
  87.  
  88. extern int errno;
  89.  
  90. /* Nonzero if we are debugging an attached outside process
  91.    rather than an inferior.  */
  92.  
  93. int attach_flag;
  94.  
  95.  
  96. /* Record terminal status separately for debugger and inferior.  */
  97.  
  98. static TERMINAL sg_inferior;
  99. static TERMINAL sg_ours;
  100.  
  101. static int tflags_inferior;
  102. static int tflags_ours;
  103.  
  104. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  105. static struct tchars tc_inferior;
  106. static struct tchars tc_ours;
  107. #endif
  108.  
  109. #ifdef TIOCGLTC
  110. static struct ltchars ltc_inferior;
  111. static struct ltchars ltc_ours;
  112. #endif
  113.  
  114. #ifdef TIOCLGET
  115. static int lmode_inferior;
  116. static int lmode_ours;
  117. #endif
  118.  
  119. #ifdef TIOCGPGRP
  120. static int pgrp_inferior;
  121. static int pgrp_ours;
  122. #else
  123. static int (*sigint_ours) ();
  124. static int (*sigquit_ours) ();
  125. #endif /* TIOCGPGRP */
  126.  
  127. /* Copy of inferior_io_terminal when inferior was last started.  */
  128. static char *inferior_thisrun_terminal;
  129.  
  130. static void terminal_ours_1 ();
  131.  
  132. /* Nonzero if our terminal settings are in effect.
  133.    Zero if the inferior's settings are in effect.  */
  134. static int terminal_is_ours;
  135.  
  136. /* Initialize the terminal settings we record for the inferior,
  137.    before we actually run the inferior.  */
  138.  
  139. void
  140. terminal_init_inferior ()
  141. {
  142.   if (remote_debugging)
  143.     return;
  144.  
  145.   sg_inferior = sg_ours;
  146.   tflags_inferior = tflags_ours;
  147.  
  148. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  149.   tc_inferior = tc_ours;
  150. #endif
  151.  
  152. #ifdef TIOCGLTC
  153.   ltc_inferior = ltc_ours;
  154. #endif
  155.  
  156. #ifdef TIOCLGET
  157.   lmode_inferior = lmode_ours;
  158. #endif
  159.  
  160. #ifdef TIOCGPGRP
  161.   pgrp_inferior = inferior_pid;
  162. #endif /* TIOCGPGRP */
  163.  
  164.   terminal_is_ours = 1;
  165. }
  166.  
  167. /* Put the inferior's terminal settings into effect.
  168.    This is preparation for starting or resuming the inferior.  */
  169.  
  170. void
  171. terminal_inferior ()
  172. {
  173.   if (remote_debugging)
  174.     return;
  175.  
  176.   if (terminal_is_ours)   /*  && inferior_thisrun_terminal == 0) */
  177.     {
  178.       fcntl (0, F_SETFL, tflags_inferior);
  179.       fcntl (0, F_SETFL, tflags_inferior);
  180.       ioctl (0, TIOCSETN, &sg_inferior);
  181.  
  182. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  183.       ioctl (0, TIOCSETC, &tc_inferior);
  184. #endif
  185. #ifdef TIOCGLTC
  186.       ioctl (0, TIOCSLTC, <c_inferior);
  187. #endif
  188. #ifdef TIOCLGET
  189.       ioctl (0, TIOCLSET, &lmode_inferior);
  190. #endif
  191.  
  192. #ifdef TIOCGPGRP
  193.       ioctl (0, TIOCSPGRP, &pgrp_inferior);
  194. #else
  195.       sigint_ours = (int (*) ()) signal (SIGINT, SIG_IGN);
  196.       sigquit_ours = (int (*) ()) signal (SIGQUIT, SIG_IGN);
  197. #endif /* TIOCGPGRP */
  198.     }
  199.   terminal_is_ours = 0;
  200. }
  201.  
  202. /* Put some of our terminal settings into effect,
  203.    enough to get proper results from our output,
  204.    but do not change into or out of RAW mode
  205.    so that no input is discarded.
  206.  
  207.    After doing this, either terminal_ours or terminal_inferior
  208.    should be called to get back to a normal state of affairs.  */
  209.  
  210. void
  211. terminal_ours_for_output ()
  212. {
  213.   if (remote_debugging)
  214.     return;
  215.  
  216.   terminal_ours_1 (1);
  217. }
  218.  
  219. /* Put our terminal settings into effect.
  220.    First record the inferior's terminal settings
  221.    so they can be restored properly later.  */
  222.  
  223. void
  224. terminal_ours ()
  225. {
  226.   if (remote_debugging)
  227.     return;
  228.  
  229.   terminal_ours_1 (0);
  230. }
  231.  
  232. static void
  233. terminal_ours_1 (output_only)
  234.      int output_only;
  235. {
  236. #ifdef TIOCGPGRP
  237.   /* Ignore this signal since it will happen when we try to set the pgrp.  */
  238.   int (*osigttou) ();
  239. #endif /* TIOCGPGRP */
  240.  
  241.   if (!terminal_is_ours)  /*   && inferior_thisrun_terminal == 0)  */
  242.     {
  243.       terminal_is_ours = 1;
  244.  
  245. #ifdef TIOCGPGRP
  246.       osigttou = (int (*) ()) signal (SIGTTOU, SIG_IGN);
  247.  
  248.       ioctl (0, TIOCGPGRP, &pgrp_inferior);
  249.       ioctl (0, TIOCSPGRP, &pgrp_ours);
  250.  
  251.       signal (SIGTTOU, osigttou);
  252. #else
  253.       signal (SIGINT, sigint_ours);
  254.       signal (SIGQUIT, sigquit_ours);
  255. #endif /* TIOCGPGRP */
  256.  
  257.       tflags_inferior = fcntl (0, F_GETFL, 0);
  258.       ioctl (0, TIOCGETP, &sg_inferior);
  259.  
  260. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  261.       ioctl (0, TIOCGETC, &tc_inferior);
  262. #endif
  263. #ifdef TIOCGLTC
  264.       ioctl (0, TIOCGLTC, <c_inferior);
  265. #endif
  266. #ifdef TIOCLGET
  267.       ioctl (0, TIOCLGET, &lmode_inferior);
  268. #endif
  269.     }
  270.  
  271. #ifdef HAVE_TERMIO
  272.   sg_ours.c_lflag |= ICANON;
  273.   if (output_only && !(sg_inferior.c_lflag & ICANON))
  274.     sg_ours.c_lflag &= ~ICANON;
  275. #else /* not HAVE_TERMIO */
  276.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  277.   if (output_only)
  278.     sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
  279. #endif /* not HAVE_TERMIO */
  280.  
  281.   fcntl (0, F_SETFL, tflags_ours);
  282.   fcntl (0, F_SETFL, tflags_ours);
  283.   ioctl (0, TIOCSETN, &sg_ours);
  284.  
  285. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  286.   ioctl (0, TIOCSETC, &tc_ours);
  287. #endif
  288. #ifdef TIOCGLTC
  289.   ioctl (0, TIOCSLTC, <c_ours);
  290. #endif
  291. #ifdef TIOCLGET
  292.   ioctl (0, TIOCLSET, &lmode_ours);
  293. #endif
  294.  
  295. #ifdef HAVE_TERMIO
  296.   sg_ours.c_lflag |= ICANON;
  297. #else /* not HAVE_TERMIO */
  298.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  299. #endif /* not HAVE_TERMIO */
  300. }
  301.  
  302. static void
  303. term_status_command ()
  304. {
  305.   register int i;
  306.  
  307.   if (remote_debugging)
  308.     {
  309.       printf_filtered ("No terminal status when remote debugging.\n");
  310.       return;
  311.     }
  312.  
  313.   printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
  314.  
  315. #ifdef HAVE_TERMIO
  316.  
  317.   printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
  318.       tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
  319.   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
  320.       sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
  321.   printf_filtered ("c_cc: ");
  322.   for (i = 0; (i < NCC); i += 1)
  323.     printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
  324.   printf_filtered ("\n");
  325.  
  326. #else /* not HAVE_TERMIO */
  327.  
  328.   printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
  329.       tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
  330.  
  331. #endif /* not HAVE_TERMIO */
  332.  
  333. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  334.   printf_filtered ("tchars: ");
  335.   for (i = 0; i < sizeof (struct tchars); i++)
  336.     printf_filtered ("0x%x ", ((char *)&tc_inferior)[i]);
  337.   printf_filtered ("\n");
  338. #endif
  339.  
  340. #ifdef TIOCGLTC
  341.   printf_filtered ("ltchars: ");
  342.   for (i = 0; i < sizeof (struct ltchars); i++)
  343.     printf_filtered ("0x%x ", ((char *)<c_inferior)[i]);
  344.   printf_filtered ("\n");
  345.   ioctl (0, TIOCSLTC, <c_ours);
  346. #endif
  347.   
  348. #ifdef TIOCLGET
  349.   printf_filtered ("lmode:  %x\n", lmode_inferior);
  350. #endif
  351. }
  352.  
  353. static void
  354. new_tty (ttyname)
  355.      char *ttyname;
  356. {
  357.   register int tty;
  358.   register int fd;
  359.  
  360. #ifdef TIOCNOTTY
  361.   /* Disconnect the child process from our controlling terminal.  */
  362.   tty = open("/dev/tty", O_RDWR);
  363.   if (tty > 0)
  364.     {
  365.       ioctl(tty, TIOCNOTTY, 0);
  366.       close(tty);
  367.     }
  368. #endif
  369.  
  370.   /* Now open the specified new terminal.  */
  371.  
  372.   tty = open(ttyname, O_RDWR);
  373.   if (tty == -1)
  374.     _exit(1);
  375.  
  376.   /* Avoid use of dup2; doesn't exist on all systems.  */
  377.   if (tty != 0)
  378.     { close (0); dup (tty); }
  379.   if (tty != 1)
  380.     { close (1); dup (tty); }
  381.   if (tty != 2)
  382.     { close (2); dup (tty); }
  383.   if (tty > 2)
  384.     close(tty);
  385. }
  386.  
  387. /* Start an inferior process and returns its pid.
  388.    ALLARGS is a string containing shell command to run the program.
  389.    ENV is the environment vector to pass.  */
  390.  
  391. #ifndef SHELL_FILE
  392. #define SHELL_FILE "/bin/sh"
  393. #endif
  394.  
  395. int
  396. create_inferior (allargs, env)
  397.      char *allargs;
  398.      char **env;
  399. {
  400.   int pid;
  401.   char *shell_command;
  402.   extern int sys_nerr;
  403.   extern char *sys_errlist[];
  404.   extern int errno;
  405.  
  406.   /* If desired, concat something onto the front of ALLARGS.
  407.      SHELL_COMMAND is the result.  */
  408. #ifdef SHELL_COMMAND_CONCAT
  409.   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + strlen (allargs) + 1);
  410.   strcpy (shell_command, SHELL_COMMAND_CONCAT);
  411.   strcat (shell_command, allargs);
  412. #else
  413.   shell_command = allargs;
  414. #endif
  415.  
  416.   /* exec is said to fail if the executable is open.  */
  417.   close_exec_file ();
  418.  
  419. #ifdef sprite
  420.   pid = fork ();
  421. #else  
  422. #if defined(USG) && !defined(HAVE_VFORK)
  423.   pid = fork ();
  424. #else
  425.   pid = vfork ();
  426. #endif
  427. #endif
  428.  
  429.   if (pid < 0)
  430.     perror_with_name ("vfork");
  431.  
  432.   if (pid == 0)
  433.     {
  434. #ifdef TIOCGPGRP
  435.       /* Run inferior in a separate process group.  */
  436.       setpgrp (getpid (), getpid ());
  437. #endif /* TIOCGPGRP */
  438.  
  439. #ifdef SET_STACK_LIMIT_HUGE
  440.       /* Reset the stack limit back to what it was.  */
  441.       {
  442.     struct rlimit rlim;
  443.  
  444.     getrlimit (RLIMIT_STACK, &rlim);
  445.     rlim.rlim_cur = original_stack_limit;
  446.     setrlimit (RLIMIT_STACK, &rlim);
  447.       }
  448. #endif /* SET_STACK_LIMIT_HUGE */
  449.  
  450.  
  451.       inferior_thisrun_terminal = inferior_io_terminal;
  452.       if (inferior_io_terminal != 0)
  453.     new_tty (inferior_io_terminal);
  454.  
  455. /* It seems that changing the signal handlers for the inferior after
  456.    a vfork also changes them for the superior.  See comments in
  457.    initialize_signals for how we get the right signal handlers
  458.    for the inferior.  */
  459. /* Not needed on Sun, at least, and loses there
  460.    because it clobbers the superior.  */
  461. /*???      signal (SIGQUIT, SIG_DFL);
  462.       signal (SIGINT, SIG_DFL);  */
  463.  
  464. #ifdef sprite
  465.       /*
  466.        * This appears to be needed on Sprite.
  467.        */
  468.       signal (SIGQUIT, SIG_DFL);
  469.       signal (SIGINT, SIG_DFL);
  470. #endif
  471.  
  472.       call_ptrace (0);
  473.       execle (SHELL_FILE, "sh", "-c", shell_command, 0, env);
  474.  
  475.       fprintf (stderr, "Cannot exec %s: %s.\n", SHELL_FILE,
  476.            errno < sys_nerr ? sys_errlist[errno] : "unknown error");
  477.       fflush (stderr);
  478.       _exit (0177);
  479.     }
  480.  
  481. #ifdef CREATE_INFERIOR_HOOK
  482.   CREATE_INFERIOR_HOOK (pid);
  483. #endif  
  484.   return pid;
  485. }
  486.  
  487. /* Kill the inferior process.  Make us have no inferior.  */
  488.  
  489. static void
  490. kill_command ()
  491. {
  492.   if (remote_debugging)
  493.     return;
  494.   if (inferior_pid == 0)
  495.     error ("The program is not being run.");
  496.   if (!query ("Kill the inferior process? "))
  497.     error ("Not confirmed.");
  498.   kill_inferior ();
  499. }
  500.  
  501. void
  502. inferior_died ()
  503. {
  504.   inferior_pid = 0;
  505.   attach_flag = 0;
  506.   mark_breakpoints_out ();
  507.   select_frame ((FRAME) 0, -1);
  508.   reopen_exec_file ();
  509.   if (have_core_file_p ())
  510.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  511.                       read_pc ()));
  512.   else
  513.     set_current_frame (0);
  514. }
  515.  
  516. #if 0 
  517. /* This function is just for testing, and on some systems (Sony NewsOS
  518.    3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
  519.    (since on this system at least sys/time.h is not protected against
  520.    multiple inclusion).  */
  521. static void
  522. try_writing_regs_command ()
  523. {
  524.   register int i;
  525.   register int value;
  526.   extern int errno;
  527.  
  528.   if (inferior_pid == 0)
  529.     error ("There is no inferior process now.");
  530.  
  531.   /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
  532.      kernel panic if we try to write past the end of the user area.
  533.      Presumably Sun will fix this bug (it has been reported), but it
  534.      is tacky to crash the system, so at least on SunOS4 we need to
  535.      stop writing when we hit the end of the user area.  */
  536.   for (i = 0; i < sizeof (struct user); i += 2)
  537.     {
  538.       QUIT;
  539.       errno = 0;
  540.       value = call_ptrace (3, inferior_pid, i, 0);
  541.       call_ptrace (6, inferior_pid, i, value);
  542.       if (errno == 0)
  543.     {
  544.       printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
  545.           i, value, value);
  546.     }
  547.       else if ((i & 0377) == 0)
  548.     printf (" Failed at 0x%x.\n", i);
  549.     }
  550. }
  551. #endif
  552.  
  553. void
  554. _initialize_inflow ()
  555. {
  556.   add_com ("term-status", class_obscure, term_status_command,
  557.        "Print info on inferior's saved terminal status.");
  558.  
  559. #if 0
  560.   add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
  561.        "Try writing all locations in inferior's system block.\n\
  562. Report which ones can be written.");
  563. #endif
  564.  
  565.   add_com ("kill", class_run, kill_command,
  566.        "Kill execution of program being debugged.");
  567.  
  568.   inferior_pid = 0;
  569.  
  570.   ioctl (0, TIOCGETP, &sg_ours);
  571.   fcntl (0, F_GETFL, tflags_ours);
  572.  
  573. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  574.   ioctl (0, TIOCGETC, &tc_ours);
  575. #endif
  576. #ifdef TIOCGLTC
  577.   ioctl (0, TIOCGLTC, <c_ours);
  578. #endif
  579. #ifdef TIOCLGET
  580.   ioctl (0, TIOCLGET, &lmode_ours);
  581. #endif
  582.  
  583. #ifdef TIOCGPGRP
  584.   ioctl (0, TIOCGPGRP, &pgrp_ours);
  585. #endif /* TIOCGPGRP */
  586.  
  587.   terminal_is_ours = 1;
  588. }
  589.  
  590. @
  591.  
  592.  
  593. 1.1
  594. log
  595. @Initial revision
  596. @
  597. text
  598. @d49 1
  599. a49 1
  600. #include <fcntl.h>
  601. d391 3
  602. d399 1
  603. d435 8
  604. @
  605.